/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.auth.service.impl;

import java.util.List;

import com.jfinal.kit.Kv;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.mapfinal.server.auth.RoleUserService;
import com.mapfinal.server.auth.model.RoleUser;
import com.mapfinal.server.auth.model.Role;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-09-23
 * @version 1.0
 * @since 1.0
 */
public class RoleUserServiceImpl extends LambkitModelServiceImpl<RoleUser> implements RoleUserService {
	
	private RoleUser DAO = null;
	
	public RoleUser dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(RoleUser.class);
		}
		return DAO;
	}

	@Override
	public boolean hasRole(long userId, int roleId) {
		// TODO Auto-generated method stub
		RoleUser roleUser = dao().findFirst(RoleUser.sql().andUserIdEqualTo(userId).andRoleIdEqualTo(roleId).example());
		return roleUser==null ? false : true;
	}

	@Override
	public boolean hasRole(long userId, String roleName) {
		// TODO Auto-generated method stub
		RoleUser roleUser = dao().findFirst(dao().getSqlPara("mapfinal.hasRoleOfName", userId, roleName));
		return roleUser==null ? false : true;
	}

	@Override
	public boolean hasAnyRoles(Long userId, String roleNames) {
		// TODO Auto-generated method stub
		roleNames = roleNames.startsWith(",") ? roleNames.substring(1) : roleNames;
		roleNames = roleNames.endsWith(",") ? roleNames.substring(0, roleNames.length()-1) : roleNames;
		Kv data = Kv.by("userid", userId).set("names", roleNames.split(","));
		List<Role> record = Role.service().dao().find(dao().getSqlPara("mapfinal.getRoleOfUserIdAndRoleNames", data));
		return record != null && record.size() > 0 ? true : false;
	}

	@Override
	public boolean hasAllRoles(Long userId, String roleNames) {
		// TODO Auto-generated method stub
		roleNames = roleNames.startsWith(",") ? roleNames.substring(1) : roleNames;
		roleNames = roleNames.endsWith(",") ? roleNames.substring(0, roleNames.length()-1) : roleNames;
		Kv data = Kv.by("userid", userId).set("names", roleNames.split(","));
		List<Role> roles = Role.service().dao().find(dao().getSqlPara("mapfinal.getRoleOfUserIdAndRoleNames", data));
		roleNames = "," + roleNames + ",";
		if(roles!=null && roles.size() > 0) {
			for (Role rule : roles) {
				roleNames.replace(","+rule.getName()+",", ",");
			}
			if(",".equals(roleNames)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}
